package com.example.night.controller;


import com.example.night.data.Good.GoodsRanking;
import com.example.night.service.ApiService.GoodsRankingService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * 商品榜单控制器
 * 提供添加、删除和获取榜单的 API
 */
@RestController
@RequestMapping("/api/ranking")
public class GoodsRankingController {

    private final GoodsRankingService goodsRankingService;

    @Autowired
    public GoodsRankingController(GoodsRankingService goodsRankingService) {
        this.goodsRankingService = goodsRankingService;
    }

    /**
     * 添加商品到榜单
     * @param requestBody 包含 rankType、productId、rank
     * @return 操作结果
     */
    @PostMapping("/add")
    public Map<String, Object> addToRanking(@RequestBody Map<String, Object> requestBody) throws JsonProcessingException, ExecutionException, InterruptedException {
        int rankType = (int) requestBody.get("rankType");
        String productId = (String) requestBody.get("productId");
        int rank = (int) requestBody.get("rank");

        goodsRankingService.addToRanking(rankType, productId, rank);
        return Map.of("status", "success", "message", "商品已添加到榜单");
    }

    /**
     * 从榜单中移除商品
     * @param rankType 榜单类型
     * @param productId 商品ID
     * @return 操作结果
     */
    @DeleteMapping("/remove")
    public Map<String, Object> removeFromRanking(@RequestParam int rankType, @RequestParam String productId) {
        boolean removed = goodsRankingService.removeFromRanking(rankType, productId);
        if (removed) {
            return Map.of("status", "success", "message", "商品已移除");
        } else {
            return Map.of("status", "fail", "message", "商品未找到");
        }
    }

    @DeleteMapping("/clear")
    public Map<String, Object> clearRanking(@RequestParam int rankType) {
        goodsRankingService.clearRanking(rankType);

        return Map.of("status", "success", "message", "已清空列表");

    }
    /**
     * 获取销量榜
     * @return 销量榜列表
     */
    @GetMapping("/sales")
    public List<GoodsRanking> getSalesRanking() {
        return goodsRankingService.getSalesRanking();
    }

    /**
     * 获取口碑榜
     * @return 口碑榜列表
     */
    @GetMapping("/reputation")
    public List<GoodsRanking> getReputationRanking(
            @RequestParam int time
    ) {
        return goodsRankingService.getPastRanking(time);
    }

    /**
     * 获取每日推荐榜
     * @return 每日推荐榜列表
     */
    @GetMapping("/daily")
    public List<GoodsRanking> getDailyRecommendationRanking() {
        return goodsRankingService.getDailyRecommendationRanking();
    }

    /**
     * 调整商品在榜单中的位置
     * @param requestBody 包含 rankType、productId、position
     * @return 操作结果
     */
    @PutMapping("/move")
    public Map<String, Object> moveToPosition(@RequestBody Map<String, Object> requestBody) {
        String productId = (String) requestBody.get("productId");
        int position = (int) requestBody.get("position");

        goodsRankingService.moveToPosition(productId, position - 1);

        return Map.of("status", "success", "message", "商品已移动到指定位置");

    }
}
